home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / Syn Text Editor 2.1.0.46 / synsetup-2.1.0.46.exe / {app} / scripts / cmnfunc.vbs < prev    next >
Text File  |  2003-08-13  |  12KB  |  436 lines

  1. '
  2. '  syn
  3. '  Copyright (C) 2000-2003, Ascher Stefan. All rights reserved.
  4. '  stievie@utanet.at, http://web.utanet.at/ascherst/
  5. '
  6. '  The contents of this file are subject to the Mozilla Public License
  7. '  Version 1.1 (the "License"); you may not use this file except in compliance
  8. '  with the License. You may obtain a copy of the License at
  9. '  http://www.mozilla.org/MPL/
  10. '
  11. '  Software distributed under the License is distributed on an "AS IS" basis,
  12. '  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
  13. '  the specific language governing rights and limitations under the License.
  14. '
  15. '  The Original Code is cmnfunc.vbs, released Sun, 26 May 2002 10:55:39 UTC.
  16. '
  17. '  The Initial Developer of the Original Code is Ascher Stefan.
  18. '  Portions created by Ascher Stefan are Copyright (C) 2000-2003 Ascher Stefan.
  19. '  All Rights Reserved.
  20. '
  21. '  Contributor(s): .
  22. '
  23. '  Alternatively, the contents of this file may be used under the terms of the
  24. '  GNU General Public License Version 2 or later (the "GPL"), in which case
  25. '  the provisions of the GPL are applicable instead of those above.
  26. '  If you wish to allow use of your version of this file only under the terms
  27. '  of the GPL and not to allow others to use your version of this file
  28. '  under the MPL, indicate your decision by deleting the provisions above and
  29. '  replace them with the notice and other provisions required by the GPL.
  30. '  If you do not delete the provisions above, a recipient may use your version
  31. '  of this file under either the MPL or the GPL.
  32. '
  33. '  You may retrieve the latest version of this file at the syn home page,
  34. '  located at http://syn.sourceforge.net/
  35. '
  36. ' $Id: cmnfunc.vbs,v 1.9.2.5 2003/08/13 00:38:45 neum Exp $
  37.  
  38. ' This file contains often used functions and procedures. You can include this
  39. ' file only in a VBScript Macro, but you may translate it into your favourite
  40. ' Language.
  41.  
  42. function IIf(Expr, TruePart, FalsePart)
  43.   ' returns the TruePart when Expr evaluates to True
  44.   if Expr then
  45.     IIf = TruePart
  46.   else
  47.     IIf = FalsePart
  48.   end if
  49. end function
  50.  
  51. ' File/Directory procedures
  52. function FileExists(FileName)
  53.   dim objFs
  54.   set objFs = CreateObject("Scripting.FileSystemObject")
  55.   FileExists = objFs.FileExists(FileName)
  56. end function
  57.  
  58. function DirExists(DirName)
  59.   dim objFs
  60.   set objFs = CreateObject("Scripting.FileSystemObject")
  61.   DirExists = objFs.FolderExists(DirName)
  62. end function
  63.  
  64. sub DeleteFile(FileName)
  65.   dim objFile, objFs
  66.   set objFs = CreateObject("Scripting.FileSystemObject")
  67.   set objFile = objFs.GetFile(FileName)
  68.   objFile.Delete
  69. end sub
  70.  
  71. sub DeleteDir(DirName)
  72.   dim objFs
  73.   set objFs = CreateObject("Scripting.FileSystemObject")
  74.   objFs.DeleteFolder DirName, true
  75. end sub
  76.  
  77. function Execute(CmdLine, Show, Wait)
  78.   dim objShell
  79.   set objShell = CreateObject("WScript.Shell")
  80.   Execute = objShell.Run(CmdLine, Show, Wait)
  81. end function
  82.  
  83. function AddBackslash(DirName)
  84.   ' adds a trailing backslash
  85.   if Right(DirName, 1) <> "\" then
  86.     AddBackslash = DirName & "\"
  87.   else
  88.     AddBackslash = DirName
  89.   end if
  90. end function
  91.  
  92. function RemoveBackslash(DirName)
  93.   ' removes a trailing backslash
  94.   if Right(DirName, 1) <> "\" then
  95.     RemoveBackslash = DirName
  96.   else
  97.     RemoveBackslash = Left(DirName, Len(DirName) - 1)
  98.   end if
  99. end function
  100.  
  101. function AddSlash(DirName)
  102.   ' adds a trailing backslash
  103.   if Right(DirName, 1) <> "/" then
  104.     AddSlash = DirName & "/"
  105.   else
  106.     AddSlash = DirName
  107.   end if
  108. end function
  109.  
  110. function RemoveSlash(DirName)
  111.   ' removes a trailing backslash
  112.   if Right(DirName, 1) <> "/" then
  113.     RemoveSlash = DirName
  114.   else
  115.     RemoveSlash = Left(DirName, Len(DirName) - 1)
  116.   end if
  117. end function
  118.  
  119. function RemoveFilename(FileName)
  120.   dim Char
  121.   RemoveFilename = FileName
  122.   while (Char <> "\") and (Len(RemoveFilename) > 0)
  123.     Char = Right(RemoveFilename, 1)
  124.     if Char <> "\" then
  125.       RemoveFilename = Left(RemoveFilename, Len(RemoveFilename) - 1)
  126.     end If
  127.   wend
  128. end function
  129.  
  130. function ExtractFilePath(FileName)
  131.   ' returns only the path from a full qualified filename without trailing
  132.   ' backslash
  133.   dim p
  134.   p = RemoveFileName(FileName)
  135.   ExtractFilePath = RemoveBackSlash(p)
  136. end function
  137.  
  138. function ExtractFilename(FileName)
  139.   ' Removes the path from a full qualified filename
  140.   ExtractFilename = Right(FileName, Len(FileName) - Len(RemoveFilename(FileName)))
  141. end function
  142.  
  143. function ShortFileName(FileName)
  144.   dim fso, f
  145.   set fso = CreateObject("Scripting.FileSystemObject")
  146.   set f = fso.GetFile(FileName)
  147.   ShortFileName = f.ShortName
  148. end function
  149.  
  150. function ShortPathName(PathName)
  151.   dim fso, f
  152.   set fso = CreateObject("Scripting.FileSystemObject")
  153.   set f = fso.GetFile(PathName)
  154.   ShortFileName = f.ShortPath
  155. end function
  156.  
  157. function GetAbsoluteFile(BaseFile, FileName)
  158.   dim tmp
  159.   dim fso, f
  160.   tmp = Curdir
  161.   CurDir = ExtractFilePath(BaseFile)
  162.   set fso = CreateObject("Scripting.FileSystemObject")
  163.   GetAbsoluteFile = fso.GetAbsolutePathName(ExtractFilePath(FileName))
  164.   GetAbsoluteFile = AddBackslash(GetAbsoluteFile) & ExtractFileName(FileName)
  165.   CurDir = tmp
  166. end function
  167.  
  168. function GetAbsolutePath(BasePath, PathName)
  169.   dim tmp
  170.   dim fso, f
  171.   tmp = Curdir
  172.   CurDir = BasePath
  173.   set fso = CreateObject("Scripting.FileSystemObject")
  174.   GetAbsolutePath = fso.GetAbsolutePathName(PathName)
  175.   CurDir = tmp
  176. end function
  177.  
  178. function TempFile
  179.   ' returns a unique filename in the temporary folder
  180.   dim fso
  181.   set fso = CreateObject("Scripting.FileSystemObject")
  182.   dim tfolder
  183.   const TemporaryFolder = 2
  184.   set tfolder = fso.GetSpecialFolder(TemporaryFolder)
  185.   TempFile = AddBackslash(tfolder.Path) & fso.GetTempName
  186. end function
  187.  
  188. function ChangeFileExt(FileName, Ext)
  189.   ' note: the dot belongs to the file extension
  190.   dim tmp
  191.   tmp = FileName
  192.   while (Right(tmp, 1) <> ".") and (tmp <> "")
  193.     tmp = Left(tmp, Len(tmp) - 1)
  194.   wend
  195.   if tmp = "" then
  196.     ChangeFileExt = FileName & Ext
  197.   else
  198.     tmp = Left(tmp, Len(tmp) - 1)
  199.     ChangeFileExt = tmp & Ext
  200.   end if
  201. end function
  202.  
  203. function ExtractFileExt(FileName)
  204.   ' Returns the file extension from a file _with_ the dot, because see above
  205.   dim tmp
  206.   tmp = FileName
  207.   while (Right(tmp, 1) <> ".") and (tmp <> "")
  208.     tmp = Left(tmp, Len(tmp) - 1)
  209.   wend
  210.   if tmp = "" then
  211.     ExtractFileExt = ""
  212.   else
  213.     ExtractFileExt = Right(FileName, Len(FileName) - Len(tmp) + 1)
  214.   end if
  215. end function
  216.  
  217. ' Common Dialogs
  218. function GetSaveFileName(FileName, Filter, DefExt, InitDir, Title, Options)
  219.   ' True -> OK
  220.   ' False -> Cancel
  221.   ' FileName returns the chosen filename
  222.   GetSaveFileName = false
  223.   with Create("TSaveDialog", Self)
  224.     .Title = Title
  225.     .InitialDir = InitDir
  226.     .DefaultExt = DefExt
  227.     .Filter = Filter
  228.     .FileName = FileName
  229.     if Options <> "" then
  230.       .Options = Options
  231.     end if
  232.     if .Execute then
  233.       FileName = .FileName
  234.       GetSaveFileName = true
  235.     end if
  236.     .Free
  237.   end with
  238. end function
  239.  
  240. function GetOpenFileName(FileName, Filter, DefExt, InitDir, Title, Options)
  241.   ' Same as above
  242.   GetOpenFileName = false
  243.   with Create("TOpenDialog", Self)
  244.     .Title = Title
  245.     .InitialDir = InitDir
  246.     .DefaultExt = DefExt
  247.     .Filter = Filter
  248.     .FileName = FileName
  249.     if Options <> "" then
  250.       .Options = Options
  251.     end if
  252.     if .Execute then
  253.       FileName = .FileName
  254.       GetOpenFileName = true
  255.     end if
  256.     .Free
  257.   end with
  258. end function
  259.  
  260. function BrowseForFolder(strPrompt, BrowseInfo, Root)
  261.   ' Shows the Browse for Folder Dialog
  262.   ' It seems you need the new Shell32.dll or something to get it to work, anyway,
  263.   ' it does not work on my machine.
  264.   dim objShell, objFolder, intColonPos, objWshShell
  265.   on error resume next
  266.   set objShell = CreateObject("Shell.Application")
  267.   if Err <> 0 then
  268.     MsgBox "Error " & Err & ": " & Err.Description, vbCritical
  269.     exit function
  270.   end if
  271.   set objFolder = objShell.BrowseForFolder(&H0, strPrompt, BrowseInfo, Root)
  272.   BrowseForFolder = objFolder.ParentFolder.ParseName(objFolder.Title).Path
  273.   if Err <> 0 then
  274.     MsgBox Err
  275.     if Err = 424 then
  276.       'Invalid Folder or Cancel
  277.       BrowseForFolder = ""
  278.     else
  279.       MsgBox "Error " & Err & ": " & Err.Description, vbCritical
  280.     end if
  281.   end if
  282. end function
  283.  
  284. ' Environment Variables
  285. function GetEnv(VarName)
  286.   ' Returns an Environment variable for the current process
  287.   dim objShell, objSysEnv
  288.   set objShell = CreateObject("WScript.Shell")
  289.   set objSysEnv = objShell.Environment("PROCESS")
  290.   GetEnv = objSysEnv(VarName)
  291. end function
  292.  
  293. sub SetEnv(VarName, Value)
  294.   ' Sets an Environment variable for the current process
  295.   dim objShell, objSysEnv
  296.   set objShell = CreateObject("WScript.Shell")
  297.   set objSysEnv = objShell.Environment("PROCESS")
  298.   objSysEnv(VarName) = Value
  299. end sub
  300.  
  301. ' Misc
  302. function AddQuotesUnless(s)
  303.   ' Adds Quotes when it contains a Space and is not already quoted
  304.   dim q
  305.   q = Chr(34)
  306.   AddQuotesUnless = Trim(s)
  307.   if (InStr(AddQuotesUnless, " ") <> 0) and ((Left(AddQuotesUnless, 1) <> q) or (Right(AddQuotesUnless, 1) <> q)) then
  308.     AddQuotesUnless = q & AddQuotesUnless & q
  309.   end if
  310. end function
  311.  
  312. function AddQuotes(s)
  313.   ' Adds Quotes in any way
  314.   dim q
  315.   q = Chr(34)
  316.   AddQuotes = q & s & q
  317. end function
  318.  
  319. function RemoveQuotes(s)
  320.   dim q
  321.   q = Chr(34)
  322.   RemoveQuotes = s
  323.   while (RemoveQuotes <> "") and (Left(RemoveQuotes, 1) = q)
  324.     RemoveQuotes = Right(RemoveQuotes, Len(RemoveQuotes) - 1)
  325.   wend
  326.   while (RemoveQuotes <> "") and (Right(RemoveQuotes, 1) = q)
  327.     RemoveQuotes = Left(RemoveQuotes, Len(RemoveQuotes) - 1)
  328.   wend
  329. end function
  330.  
  331. ' Pascal String Procs
  332. sub Delete(s, index, count)
  333.   dim l, r
  334.   l = Left(s, index - 1)
  335.   r = Mid(s, index + count, Len(s) - (index + count) + 1)
  336.   s = l & r
  337. end sub
  338.  
  339. sub Insert(source, s, index)
  340.   dim l, r
  341.   l = Left(source, index)
  342.   r = Mid(source, index + 1, Len(source) - index + 1)
  343.   source = l & s & r
  344. end sub
  345.  
  346. sub StringToFile(String_, FileName)
  347.   dim fso, f
  348.   set fso = CreateObject("Scripting.FileSystemObject")
  349.   set f = fso.CreateTextFile(FileName, true)
  350.   f.Write(String_)
  351.   f.Close
  352. end sub
  353.  
  354. sub FileWriteLine(String_, FileName, Line)
  355.   const ForWriting = 2
  356.   dim fso, f, i, ts
  357.   set fso = CreateObject("Scripting.FileSystemObject")
  358.   set f = fso.GetFile(FileName)
  359.   set ts = f.OpenAsTextStream(ForWriting, -1)
  360.   while (i < Line) or (not f.AtEndOfStream)
  361.     ts.SkipLine
  362.   wend
  363.   ts.WriteLine(String_)
  364.   ts.Close
  365. end sub
  366.  
  367. function FileToString(FileName)
  368.   const ForReading = 1
  369.   Dim fso, f
  370.   Set fso = CreateObject("Scripting.FileSystemObject")
  371.   Set f = fso.OpenTextFile(FileName, ForReading)
  372.   FileToString = f.ReadAll
  373. end function
  374.  
  375. function FileReadLine(FileName, Line)
  376.   const ForReading = 1
  377.   Dim fso, f, i, ts
  378.   Set fso = CreateObject("Scripting.FileSystemObject")
  379.   set f = fso.GetFile(FileName)
  380.   set ts = f.OpenAsTextStream(ForReading, -2)
  381.   while i < Line
  382.     if ts.AtEndOfStream then
  383.       FileReadLine = ""
  384.       exit function
  385.     end if
  386.     ts.SkipLine
  387.   wend
  388.   FileReadLine = ts.ReadLine
  389.   ts.Close
  390. end function
  391.  
  392. ' Registry
  393. function RegGetSettings(Key, Default)
  394.   dim wsh
  395.   set wsh = CreateObject("WScript.Shell")
  396.   on error resume next
  397.   RegGetSettings = wsh.RegRead(Key)
  398.   if Err <> 0 then
  399.     ' Value does not exist, probably
  400.     RegGetSettings = Default
  401.   end if
  402. end function
  403.  
  404. sub RegSetSettings(Key, Value)
  405.   dim wsh
  406.   set wsh = CreateObject("WScript.Shell")
  407.   wsh.RegWrite Key, Value
  408. end sub
  409.  
  410. sub RegDelSettings(Key)
  411.   dim wsh
  412.   set wsh = CreateObject("WScript.Shell")
  413.   wsh.RegDelete Key
  414. end sub
  415.  
  416. function RegValueExists(Key)
  417.   dim wsh, dummy
  418.   set wsh = CreateObject("WScript.Shell")
  419.   on error resume next
  420.   dummy = wsh.RegRead(Key)
  421.   RegValueExists = (Err = 0)
  422. end function
  423.  
  424. ' Misc
  425. function CheckSave
  426.   ' Asks to save modified files
  427.   dim i, m
  428.   CheckSave = true
  429.   for i = 0 to Documents.Count - 1
  430.     if Documents(i).Modified then
  431.       CheckSave = Documents.SaveAll(true)
  432.       exit for
  433.     end if
  434.   next
  435. end function
  436.